<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$this->model('model')->checkvip();
if($_RQ['type']){
	$this->session('type',$_RQ['type']);
}
$type=$this->session('type');
if($_RQ['op']=='main'){
	$user=pdo_get('jy_onlineedu_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	set_html('title','我的课程');
	$return['params']=array('type'=>$type?:'wechat','keyword'=>'');
	if($_RQ['keyword']){
		$return['params']['keyword']=$_RQ['keyword'];
	}
	$return['video_status']=$this->video_status;
	exi($return);
}
if($_RQ['op']=='getlist'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);	
	}
	if($_RQ['keyword']){
		$params['title like']='%'.$_RQ['keyword'].'%';
		$return['params']['keyword']=$_RQ['keyword'];		
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_onlineedu_video',$params,array($pindex,$psize),$total,$fields,'','updatetime desc');
	foreach($list as &$kv){
			$kv['thumb']=tomedia($kv['thumb']);
	}	
	//按点赞数排序作品
	if($list){
		$sum=count($list);
		foreach($list as &$item){
			$item['updatetime']=$item['updatetime']?:$item['createtime'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['thumb']=tomedia($item['thumb']);
			$lasttime=TIMESTAMP-$item['updatetime'];
			if($lasttime>24*3600){
				$item['updatetimetitle']=intval($lasttime/24*3600).'天前';
			}elseif($lasttime>=3600){
				$item['updatetimetitle']=intval($lasttime/3600).'小时前';
			}else{
				$item['updatetimetitle']=intval($lasttime/60).'分钟前';
			}
			$item['likenum']=pdo_count('jy_onlineedu_video_log',array('video_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'islike'=>1),'id');
			$quan_log=pdo_get('jy_onlineedu_video_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$item['id']));
			if($quan_log['id']){
				if($quan_log['isadd']==1){
					$item['islook']=2;
				}else{
					$item['islook']=1;
				}
			}
			
		}
	}
	$return['list']=$list;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_onlineedu_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3);
	}
	$item['videos']=$item['videos']?json_decode($item['videos'],true):array();
	set_html('title','编辑课程');
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edit'=>array(
					array('name'=>'title','title'=>'课程名称','type'=>'string'),
					array('name'=>'thumb','title'=>'课程封面','type'=>'image','help'=>'留空会自动截取视频第一张图片/图集第一张'),
					array('name'=>'group_id','title'=>'分类','type'=>'select','options'=>'groups'),
					array(
					'type'=>'array',
					'title'=>'课程列表',
					'name'=>'videos',
					'add_tip'=>'添加一个视频课程',
									'list'=>array(
										array('type'=>'string','name'=>'title','title'=>'课程名称'),
										array('type'=>'image','name'=>'thumb','title'=>'课程LOGO'),
										//array('type'=>'string','name'=>'video','title'=>'视频地址'),
									),
									'details'=>array(
										array('type'=>'string','name'=>'title','title'=>'课程名称'),
										array('type'=>'image','name'=>'thumb','title'=>'课程LOGO'),
										array('name'=>'desc','title'=>'课程介绍','type'=>'text'),
										array('type'=>'video','name'=>'video','title'=>'视频地址','help'=>'也可以直接填远程网络地址'),
									)
					),
					array('name'=>'desc','title'=>'课程介绍','type'=>'text'),
					array('name'=>'type','title'=>'课程类型','type'=>'radio','options'=>'types'),
					array('name'=>'price','title'=>'课程价格','type'=>'string','show'=>array('type'=>1)),
					array('type'=>'richtext','title'=>'课程内容','name'=>'content' , 'help'=>''),
					array('name'=>'readnum','title'=>'阅读人数','type'=>'string'),
					array('name'=>'times','title'=>'用时','type'=>'unitstring','unit'=>'小时'),
					array('name'=>'times_title','title'=>'学习时间自定义','type'=>'string','help'=>'默认显示：学习时间'),
					array('name'=>'star','title'=>'难易星数','type'=>'radio','options'=>'star'),
					array('name'=>'noshow','title'=>'不显示？','type'=>'switch'),
		)
	);
	$return['star']=array(1=>'1星',2=>'2星',3=>'3星',4=>'4星',5=>'5星');
	$return['types']=$this->video_types;
	$return['groups']=pdo_getall('jy_onlineedu_video_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写课程名称！','error');
	}
	if(!$postdata['group_id']){
		exi('请填写课程分类！','error');
	}
	$status=$return['pluginset']['needsh']==1?0:1;
	$data=array(
		'title'=>trim($postdata['title']),
		'thumb'=>trim($postdata['thumb']),
		'type'=>intval($postdata['type']),
		'desc'=>trim($postdata['desc']),
		'video'=>$postdata['videos'][0]?$postdata['videos'][0]['video']:'',
		'videos'=>ijson_encode($postdata['videos']),
		'content'=>trim($postdata['content']),
		'readnum'=>intval($postdata['readnum']),
		'group_id'=>intval($postdata['group_id']),
		'ewm'=>trim($postdata['ewm']),
		'ewmurl'=>trim($postdata['ewmurl']),
		'status'=>$status,
		'price'=>trim($postdata['price']),
		'star'=>intval($postdata['star']),
		'times'=>intval($postdata['times']),
		'times_title'=>trim($postdata['times_title']),
		'noshow'=>intval($postdata['noshow']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_onlineedu_video',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		$data['updatetime']=TIMESTAMP;
		pdo_insert('jy_onlineedu_video',$data);
	}
	exi('操作成功！','','jy_onlineedu/myvideo/main/');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('课程不存在哦！','error');
	}
	pdo_delete('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功！');

}